package com.huluobo.a2111douyindemo.live;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.mvvm.base.model.BaseRepo;
import com.huluobo.mvvm_base.mvvm.bean.FollowBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.GiftBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.Money;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/3/5.
 * Repo是数据仓库,所以所有数据相关的操作都需要放到Repo中
 * 网络请求的本质:通过接口,请求后台的数据库,返回数据(比如登录接口,通过传递username,password,返回一个userBean)
 * 本地请求数据库是同样的道理,也是通过传递(username)返回一个UserBean
 * 所以甭管是网络请求或者是数据库请求,所有数据相关的操作,都放在Repo中
 */
public class LiveRepo extends BaseRepo {
    /**
     * 关注作者
     *
     * @param body    传递json
     * @param success 成功的livedata
     * @param failed  失败的livedata
     */
    public void followAuth(RequestBody body, MutableLiveData<FollowBeanResult> success,
                           MutableLiveData<String> failed) {
        apiService.followAuth(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowBeanResult followBeanResult) {
                        success.setValue(followBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void noFollowAuth(RequestBody body, MutableLiveData<FollowBeanResult> success,
                             MutableLiveData<String> failed) {
        apiService.noFollowAuth(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowBeanResult followBeanResult) {
                        success.setValue(followBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getGiftList(MutableLiveData<GiftBeanResult> success, MutableLiveData<String> failed) {
        apiService.getGiftList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GiftBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GiftBeanResult giftBeanResult) {
                        success.setValue(giftBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 查询用户余额
     *
     * @param username 用户名
     * @param success  查询成功
     * @param failed   查询失败
     */
    public void queryUserMoney(String username, MutableLiveData<Money> success, MutableLiveData<String> failed) {
        if (moneyDao.queryUserMoney(username) != null) {
            success.setValue(moneyDao.queryUserMoney(username));
        } else {
            failed.setValue("查询用户余额失败");
        }
    }

    public void updateUserMoney(Money money) {
        moneyDao.updateUserMoney(money);
    }
}
